package com.xts.weinfo.presenter;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.xts.weinfo.R;
import com.xts.weinfo.base.BaseApp;
import com.xts.weinfo.base.BasePresenter;
import com.xts.weinfo.base.Constants;
import com.xts.weinfo.bean.LoginBean;
import com.xts.weinfo.model.LoginModel;
import com.xts.weinfo.net.LoginService;
import com.xts.weinfo.net.ResultCallBack;
import com.xts.weinfo.util.SpUtil;
import com.xts.weinfo.view.LoginView;

public class LoginPresenter extends BasePresenter<LoginView> {
    private LoginModel mLoginModel;
    //每次都得实现这个构造,好烦呀
    //分析这个构造做啥了,能不能把他的任务分配给别人搞
    //保存了view的对象
    /*public LoginPresenter(LoginView view) {
        super(view);
    }*/
    //所有的p层都需要有构造接收v层对象,往父类里面抽
    /*private final LoginView mView;

    public LoginPresenter(LoginView view) {
        this.mView = view;
    }*/

    public void login(String name, String psd){
        /*mLoginModel.login(name,psd, new ResultCallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                //if (loginBean.getCode().equals(LoginService.LOGIN_SUCCESS_CODE)){
                //开发使用这种,因为loginBean.getCode()后端给的,有可能是null
                if (LoginService.LOGIN_SUCCESS_CODE.equals(loginBean.getCode())){
                    //成功
                    mView.loginSuccess();
                    //保存用户信息
                    saveUserData(loginBean);
                }else {
                    //失败
                    mView.showToast(BaseApp.getRes().getString(R.string.login_fail));
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });*/

        //登录环信
        mLoginModel.loginEaseMod(name,psd, new ResultCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                mView.showToast(s);
                mView.loginSuccess();
            }

            @Override
            public void onFail(String msg) {
                mView.showToast(msg);
            }
        });
    }

    ////保存用户信息
    private void saveUserData(LoginBean loginBean) {
        LoginBean.UserBean user = loginBean.getUser();
        String token = loginBean.getToken();

        SpUtil.setParam(Constants.TOKEN,token);
        SpUtil.setParam(Constants.USERNAME,user.getName());
        SpUtil.setParam(Constants.USERID,user.getUserid());
        SpUtil.setParam(Constants.PHOTO,user.getHeaderpic());
    }

    @Override
    protected void initModel() {
        mLoginModel = new LoginModel();
        addModel(mLoginModel);
    }

    public void loginAccess(SHARE_MEDIA platform, final String uid) {
        String typeId = LoginService.TYPE_QQ;
        if (platform == SHARE_MEDIA.QQ){
            typeId = LoginService.TYPE_QQ;
        }else if (platform == SHARE_MEDIA.WEIXIN){
            typeId = LoginService.TYPE_WECHAT;
        }else if (platform == SHARE_MEDIA.SINA){
            typeId = LoginService.TYPE_SINA;
        }
        final String finalTypeId = typeId;
        mLoginModel.loginAccess(uid,typeId, new ResultCallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean) {
                //开发使用这种,因为loginBean.getCode()后端给的,有可能是null
                if (LoginService.LOGIN_SUCCESS_CODE.equals(loginBean.getCode())){
                    //成功
                    mView.loginSuccess();
                    //保存用户信息
                    saveUserData(loginBean);
                }else {
                    //不是失败
                    //是因为拿到的三方唯一标识并没有在我们服务创建过用户,我们需要创建一个
                    //跳转到完善资料页面,
                    //完善资料页面和注册页面长得差不多,所以可以服用注册页面
                    mView.inputUserInfo(uid, finalTypeId);
                    mView.showToast(BaseApp.getRes().getString(R.string.input_info));
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
